package tool

import (
	"encoding/json"
	"io/ioutil"
	"net/http"
	"strings"
)

func HttpPost(url string, contentType string, data map[string]interface{}) (resp map[string]interface{}, err error) {
	dataStr, _ := json.Marshal(data)
	resphttp, err := http.Post(url, contentType, strings.NewReader(string(dataStr)))
	if err != nil {
		return nil, err
	}
	defer resphttp.Body.Close()
	b, err := ioutil.ReadAll(resphttp.Body)
	if err != nil {
		return nil, err
	}

	err = json.Unmarshal(b, &resp)
	if err != nil {
		return nil, err
	}

	return resp, nil
}

func HttpGet(url string) (resp map[string]interface{}, err error) {
	resphttp, err := http.Get(url)
	if err != nil {
		return nil, err
	}
	defer resphttp.Body.Close()
	b, err := ioutil.ReadAll(resphttp.Body)
	if err != nil {
		return nil, err
	}

	err = json.Unmarshal(b, &resp)
	if err != nil {
		return nil, err
	}

	return resp, nil
}
